package com.neuedu.his.service;

import com.neuedu.his.dao.ConstantItemDao;
import com.neuedu.his.dao.ConstantTypeDao;
import com.neuedu.his.entity.ConstantItem;
import com.neuedu.his.entity.ConstantType;
import com.neuedu.his.framework.BaseService;
import com.neuedu.his.framework.PageInfo;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 项目：      his
 * 类名:       RoleService
 * 创建时间：  2020/12/23  8:52
 * 描述 :     用户测试类
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
public class ConstantItemService implements BaseService<ConstantItem> {
    private ConstantItemDao constantItemDao = new ConstantItemDao();

    @Override
    public List<ConstantItem> queryList(HttpServletRequest request) {

        String code = request.getParameter("code");
        String sql = null;
        if(code == null || "".equals(code.trim())){
           sql = " select * from constant_item ";
            return constantItemDao.selectList(sql);
        }else{
            sql = " SELECT item.*  FROM  constant_item item  JOIN constant_type t   ON item.type_id = t.id where item.active=1 and t.code= ? ";
            return constantItemDao.selectList(sql,code);
        }
    }


    /**
     * 分页查询
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @Override
    public PageInfo queryListByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        PageInfo pageInfo = getPageInfo(request);

        String name = request.getParameter("name");

        if(name == null ){
            name = "";
        }

        name = "%"+name.trim()+"%";


        String sql = " select * from constant_item  where  name like ?   limit ?  , ?  ";

        int start = (pageInfo.getPageNo()-1) * pageInfo.getPageSize() ;
        List<ConstantItem> list =  constantItemDao.selectList(sql,name, start,pageInfo.getPageSize());

        String sql2 = "  select count(1) from constant_item    where name like ?   ";
        long count =  constantItemDao.selectCount(sql2,name);

        pageInfo.setList(list);
        pageInfo.setTotalCount((int) count);

        return pageInfo;
    }

    @Override
    public ConstantItem queryById(Integer id) {
        return constantItemDao.selectById(id);
    }

    @Override
    public boolean edit(ConstantItem constantItem) {
        return false;
    }


    /**
     * 逻辑删除，没有真正删除数据
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {

        return  constantItemDao.deleteById(id);
    }

    @Override
    public boolean save(ConstantItem constantItem) {
        return constantItemDao.insert(constantItem);
    }

    @Override
    public boolean batchdel(String[] ids) throws Exception {
        return false;
    }

    public boolean update(ConstantItem constantItem) {
        return constantItemDao.update(constantItem);
    }

}
